<template>
  <div class="form-demo">
    <h2>Element UI Form 表单示例</h2>

    <!-- 表单区域 -->
    <el-form 
      ref="form"
      :model="formData"
      :rules="rules"
      label-width="100px"
      class="demo-form">
      
      <!-- 基础输入框 -->
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名"></el-input>
      </el-form-item>

      <!-- 密码输入框 -->
      <el-form-item label="密码" prop="password">
        <el-input 
          v-model="formData.password" 
          type="password"
          placeholder="请输入密码"
          show-password>
        </el-input>
      </el-form-item>

      <!-- 选择器 -->
      <el-form-item label="角色" prop="role">
        <el-select v-model="formData.role" placeholder="请选择角色">
          <el-option 
            v-for="item in roleOptions" 
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <!-- 多选框组 -->
      <el-form-item label="权限" prop="permissions">
        <el-checkbox-group v-model="formData.permissions">
          <el-checkbox 
            v-for="item in permissionOptions" 
            :key="item.value"
            :label="item.value">
            {{ item.label }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <!-- 单选框组 -->
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio 
            v-for="item in statusOptions"
            :key="item.value" 
            :label="item.value">
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <!-- 日期选择器 -->
      <el-form-item label="生效日期" prop="effectiveDate">
        <el-date-picker
          v-model="formData.effectiveDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>

      <!-- 开关 -->
      <el-form-item label="启用通知">
        <el-switch v-model="formData.enableNotification"></el-switch>
      </el-form-item>

      <!-- 文本域 -->
      <el-form-item label="备注" prop="remark">
        <el-input 
          type="textarea"
          v-model="formData.remark"
          :rows="3"
          placeholder="请输入备注信息">
        </el-input>
      </el-form-item>

      <!-- 表单操作按钮 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'FormDemo',

  data() {
    return {
      // 表单数据
      formData: {
        username: '',
        password: '',
        role: '',
        permissions: [],
        status: 1,
        effectiveDate: '',
        enableNotification: false,
        remark: ''
      },

      // 表单验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        permissions: [
          { type: 'array', required: true, message: '请至少选择一个权限', trigger: 'change' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ],
        effectiveDate: [
          { required: true, message: '请选择生效日期', trigger: 'change' }
        ],
        remark: [
          { max: 200, message: '长度不能超过200个字符', trigger: 'blur' }
        ]
      },

      // 选项数据
      roleOptions: [
        { value: 'admin', label: '管理员' },
        { value: 'editor', label: '编辑者' },
        { value: 'viewer', label: '访问者' }
      ],
      permissionOptions: [
        { value: 'create', label: '创建' },
        { value: 'read', label: '读取' },
        { value: 'update', label: '更新' },
        { value: 'delete', label: '删除' }
      ],
      statusOptions: [
        { value: 1, label: '启用' },
        { value: 0, label: '禁用' }
      ]
    }
  },

  methods: {
    // 提交表单
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$message.success('表单验证通过')
          console.log('表单数据:', this.formData)
        } else {
          this.$message.error('表单验证失败')
          return false
        }
      })
    },

    // 重置表单
    resetForm() {
      this.$refs.form.resetFields()
      this.$message.info('表单已重置')
    }
  }
}
</script>

<style scoped>
.form-demo {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

h2 {
  color: #303133;
  margin: 0 0 20px 0;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.demo-form {
  margin-top: 20px;
}

</style> 